package com.pany.rememberpass.Adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.pany.rememberpass.R;
import com.pany.rememberpass.bean.ItemBean;

import java.util.List;

/**
 * @name： RememberPass
 * @author： zs
 * @time： 2022/5/9 0009
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<ItemBean> itemBeanList;
    private Context mContent;
    private LayoutInflater mLayoutInflater;

    public MyAdapter(Context context, List<ItemBean> data) {
        this.itemBeanList = data;
        this.mContent = context;
        mLayoutInflater = LayoutInflater.from(mContent);

    }

    @Override
    public int getItemCount() {
        return itemBeanList != null ? itemBeanList.size() : 0;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder = null;
        if (myViewHolder == null) {
            View view = mLayoutInflater.inflate(R.layout.list_item_layout, parent, false);
            Log.d("TAG", "onCreateViewHolder: --11111111111111------------");
            myViewHolder = new MyViewHolder(view);
        }


        Log.d("TAG", "onCreateViewHolder: --------------");

        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {

        int width = ((Activity) holder.iv_img.getContext()).getWindowManager().getDefaultDisplay().getWidth();
        ViewGroup.LayoutParams params = holder.iv_img.getLayoutParams();

        ItemBean itemBean = itemBeanList.get(position);

        holder.tv_title.setText(itemBean.getTitle());
        holder.tv_content.setText(itemBean.getContent());
        holder.iv_img.setImageResource(itemBean.getImgRId());

        BitmapFactory.Options options = new BitmapFactory.Options();
        BitmapFactory.decodeResource(mContent.getResources(), itemBean.getImgRId(), options);

        //获取图片的宽高
        int outWidth = options.outWidth;
        int height = options.outHeight;

        //设置图片的相对于屏幕的宽高比
//        params.width = outWidth / 2;
//        params.height = (int) (200 + Math.random() * 400);
//        holder.iv_img.setLayoutParams(params);

        if (outWidth > height) {
            outWidth = height;
        } else {
            height = outWidth;
        }

        Log.i("TAG", "第 " + position + " 个的图片的宽度: " + outWidth + "图片的高度: " + height);

        holder.list_item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContent, " 点击了 第 " + position, Toast.LENGTH_SHORT).show();
            }
        });

    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_img;
        TextView tv_content;
        TextView tv_title;
        RelativeLayout list_item_layout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            list_item_layout = itemView.findViewById(R.id.list_item_layout);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_title = itemView.findViewById(R.id.tv_title);


        }
    }
}
